package com.example.chatroom.config;

import com.example.chatroom.api.TestWebSocketAPI;
import com.example.chatroom.api.WebSocketAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.*;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

//使得这个类能被spring解析
@Configuration
@EnableWebSocket
public  class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestWebSocketAPI testWebSocketAPI;
    @Autowired
    private WebSocketAPI webSocketAPI;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry){
        //将创建好的Handler注册到对应的路径上
        //这样，当浏览器的websocket的请求路径为“/test”时，就会调用TestWebSocketAPI的方法
        registry.addHandler(testWebSocketAPI,"/test");
        registry.addHandler(webSocketAPI,"/sendMessage")
                //通过增加这个特殊的拦截器，就可以将HttpSession里面添加的属性
                //在webSocketSession中也增加
        .addInterceptors(new HttpSessionHandshakeInterceptor());
    }

}
